package it.yuzuojian.com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.yuzuojian.com.domain.CommonResult;
import it.yuzuojian.com.domain.SongAndSongList;
import it.yuzuojian.com.service.SongAndSongListService;
import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.plugin.com.event.COMEventHandler;

import java.util.List;

@RestController
@RequestMapping("/songAndSongList")
public class SongAndSongListController {
    @Autowired
    private SongAndSongListService songAndSongListService;
    @GetMapping("/findBySongListId")
    public CommonResult findBySongListId(Integer songListId){
        List<SongAndSongList> list=null;
        try {
            list = songAndSongListService.selectBySongListId(songListId);
            return new CommonResult<List<SongAndSongList>>(1,"数据加载成功！",list);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult<List<SongAndSongList>>(0,"数据加载失败！",list);
        }
    }
    @PostMapping("/add")
    public CommonResult insert(@RequestBody String requestJson){
        if(requestJson==null||requestJson.length()==0){
            return new CommonResult<Boolean>(0,"json数据传递失败！",false);
        }
        System.out.println(requestJson);
        ObjectMapper objectMapper = new ObjectMapper();
        SongAndSongList songAndSongList=null;
        try {
            songAndSongList = objectMapper.readValue(requestJson, SongAndSongList.class);
            System.out.println(songAndSongList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"json数据转换失败！",false);
        }
        if(songAndSongListService.insert(songAndSongList)){
            return new CommonResult<Boolean>(1,"添加成功！",true);
        }
        return new CommonResult<Boolean>(0,"添加失败！",false);
    }
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String requestJson){
        if(requestJson==null||requestJson.length()==0){
            return new CommonResult<Boolean>(0,"json数据传递失败！",false);
        }
        System.out.println(requestJson);
        JsonNode jsonNode=null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
             jsonNode = objectMapper.readTree(requestJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"json数据转换失败！",false);
        }
        String[] songIds = jsonNode.get("songIds").toString()
                .replace("[", "").replace("]", "").trim().split(",");
        String[] songListIds = jsonNode.get("songListIds").toString()
                .replace("[", "").replace("]", "").trim().split(",");
        Integer[] songData=new Integer[songIds.length];
        Integer[] songListData=new Integer[songListIds.length];
        for (int i = 0; i < songIds.length; i++) {
            songData[i]=Integer.parseInt(songIds[i]);
            songListData[i]=Integer.parseInt(songListIds[i]);
        }
        if(songAndSongListService.delete(songData,songListData)){
            return new CommonResult<Boolean>(1,"删除成功！",true);
        }
        return new CommonResult<Boolean>(0,"删除失败！",false);
    }
}
